namespace Beauty.Net.Middleware.Core;

/// <summary>
/// 通用API响应模型
/// </summary>
public class ApiResponse
{
    /// <summary>
    /// 状态码
    /// </summary>
    public int Code { get; set; }
    
    /// <summary>
    /// 消息
    /// </summary>
    public string Message { get; set; }
    
    /// <summary>
    /// 数据
    /// </summary>
    public object? Data { get; set; }
    
    /// <summary>
    /// 时间戳
    /// </summary>
    public long Timestamp { get; set; }
    
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success => Code == 200;
    
    /// <summary>
    /// 构造函数
    /// </summary>
    public ApiResponse()
    {
        Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
    }
    
    /// <summary>
    /// 创建成功响应
    /// </summary>
    public static ApiResponse SuccessResponse(object? data = null, string message = "success")
    {
        return new ApiResponse
        {
            Code = 200,
            Message = message,
            Data = data
        };
    }
    
    /// <summary>
    /// 创建失败响应
    /// </summary>
    public static ApiResponse ErrorResponse(int code, string message)
    {
        return new ApiResponse
        {
            Code = code,
            Message = message,
            Data = null
        };
    }
}